package cn.xiaoliu.pet.controller;

import cn.xiaoliu.basic.exception.BusinessException;
import cn.xiaoliu.basic.utils.AjaxResult;
import cn.xiaoliu.basic.utils.LoginContext;
import cn.xiaoliu.basic.utils.PageList;
import cn.xiaoliu.pet.domain.Pet;
import cn.xiaoliu.pet.domain.SearchMasterMsg;
import cn.xiaoliu.pet.query.SearchMasterMsgQuery;
import cn.xiaoliu.pet.service.ISearchMasterMsgService;
import cn.xiaoliu.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    /**
     * 发布寻主信息接口
     * @param masterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg masterMsg, HttpServletRequest request){
        try {
            seachMasterMsgService.publish(masterMsg, LoginContext.getLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }

    //查询待处理信息 state=0  通过有没有shop_id来区分是平台或者店铺
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.pending(query,LoginContext.getLogininfo(request));
    }
    //查询已处理信息 state=0 通过有没有shop_id来区分是平台或者店铺
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.processed(query,LoginContext.getLogininfo(request));
    }
    //查询用户自己发布的信息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> queryUserText(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.queryUserText(query,LoginContext.getLogininfo(request));
    }

    /**
     *处理寻主信息
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet, LoginContext.getLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }

    @GetMapping
    public List<SearchMasterMsg> getAll(){

        return seachMasterMsgService.queryAll();
    }

}
